#ifndef __BNO08X_H
#define __BNO08X_H

#ifdef __cplusplus
extern "C"{
#endif

#include "user_i2c.h"

/*********** 状态码 ***********/
#define IMU_OK 0
#define IMU_NO_DATA_INPUT 1
#define IMU_NOT_READY 2
#define IMU_INIT_ERROR 3
#define IMU_DO_NOT_SUPPORT 4
#define IMU_DATA_LENGTH_OUT_OF_RANGE 5

/*********** 寄存器定义 ***********/
#define BNO085_DEFAULT_ADDRESS 0x4B

#define I2C_BUFFER_LENGTH I2C_BUFFER_SIZE//bno_i2c缓存大小
#define MAX_PACKET_SIZE 32  // Packets can be up to 32k but we don't have that much RAM.
#define MAX_METADATA_SIZE 9  // This is in words. There can be many but we mostly only care about the first 9 (Qs, range, etc)

#define CHANNEL_COMMAND  0
#define CHANNEL_EXECUTABLE  1
#define CHANNEL_CONTROL  2
#define CHANNEL_REPORTS  3
#define CHANNEL_WAKE_REPORTS  4
#define CHANNEL_GYRO  5

// All the ways we can configure or talk to the BNO080, figure 34, page 36 reference manual
// These are used for low level communication with the sensor, on channel 2
#define SHTP_REPORT_COMMAND_RESPONSE 0xF1
#define SHTP_REPORT_COMMAND_REQUEST 0xF2
#define SHTP_REPORT_FRS_READ_RESPONSE 0xF3
#define SHTP_REPORT_FRS_READ_REQUEST 0xF4
#define SHTP_REPORT_PRODUCT_ID_RESPONSE 0xF8
#define SHTP_REPORT_PRODUCT_ID_REQUEST 0xF9
#define SHTP_REPORT_BASE_TIMESTAMP 0xFB
#define SHTP_REPORT_SET_FEATURE_COMMAND 0xFD

// All the different sensors and features we can get reports from
// These are used when enabling a given sensor
#define SENSOR_REPORTID_ACCELEROMETER 0x01
#define SENSOR_REPORTID_GYROSCOPE 0x02
#define SENSOR_REPORTID_MAGNETIC_FIELD 0x03
#define SENSOR_REPORTID_LINEAR_ACCELERATION 0x04
#define SENSOR_REPORTID_ROTATION_VECTOR 0x05
#define SENSOR_REPORTID_GRAVITY 0x06
#define SENSOR_REPORTID_GAME_ROTATION_VECTOR 0x08
#define SENSOR_REPORTID_GEOMAGNETIC_ROTATION_VECTOR 0x09
#define SENSOR_REPORTID_TAP_DETECTOR 0x10
#define SENSOR_REPORTID_STEP_COUNTER 0x11
#define SENSOR_REPORTID_STABILITY_CLASSIFIER 0x13
#define SENSOR_REPORTID_PERSONAL_ACTIVITY_CLASSIFIER 0x1E
#define SENSOR_REPORTID_ARVR_STABILIZED_RV 0x28
#define SENSOR_REPORTID_ARVR_STABILIZED_GRV 0x29

// Record IDs from figure 29, page 29 reference manual
// These are used to read the metadata for each sensor type
#define FRS_RECORDID_ACCELEROMETER 0xE302
#define FRS_RECORDID_GYROSCOPE_CALIBRATED 0xE306
#define FRS_RECORDID_MAGNETIC_FIELD_CALIBRATED 0xE309
#define FRS_RECORDID_ROTATION_VECTOR 0xE30B

// Command IDs from section 6.4, page 42
// These are used to calibrate, initialize, set orientation, tare etc the sensor
#define COMMAND_ERRORS 1
#define COMMAND_COUNTER 2
#define COMMAND_TARE 3
#define COMMAND_INITIALIZE 4
#define COMMAND_DCD 6
#define COMMAND_ME_CALIBRATE 7
#define COMMAND_DCD_PERIOD_SAVE 9
#define COMMAND_OSCILLATOR 10
#define COMMAND_CLEAR_DCD 11

#define CALIBRATE_ACCEL 0
#define CALIBRATE_GYRO 1
#define CALIBRATE_MAG 2
#define CALIBRATE_PLANAR_ACCEL 3
#define CALIBRATE_ACCEL_GYRO_MAG 4
#define CALIBRATE_STOP 5

/*********** IMU常量 ***********/
#define ROTATION_VECTOR_Q1 14
#define ACCELEROMETER_Q1 8
#define LINEAR_ACCELEROMETER_Q1 8
#define GYRO_Q1 9
#define MAGNETOMETER_Q1 4

/********************全局函数声明********************/
int8_t bno08x_init(void);
int8_t bno08x_read_all(float* qr,float* qi,float* qj,float* qk);

#ifdef __cplusplus
}
#endif

#endif